﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using UserSystemApi.Entity;

namespace UserSystemApi.Core
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity  :BaseEntity
    {
        private readonly UserSystemContext _context;
        //依赖注入
        public Repository(UserSystemContext context)
        {
            _context = context;
        }

        public async Task<bool> DeleteAsync(int id)
        {
            var entity = await _context.Set<TEntity>().FirstOrDefaultAsync(m => m.Id == id);

            if (entity == null)
            {
                return false; // 实体不存在，删除失败
            }
            _context.Set<TEntity>().Remove(entity);
            await _context.SaveChangesAsync();
            return true; // 成功删除实体
        }
 

        public async Task<int> InsertResultIdAsync(TEntity entity)
        {
            var res = _context.Set<TEntity>().Add(entity);
             await _context.SaveChangesAsync(); ;
            return res.Entity.Id;
        }
        public async Task<TEntity> InsertAsync(TEntity entity)
        {
            var dbSet = _context.Set<TEntity>();
            var res = (await dbSet.AddAsync(entity)).Entity;
            await _context.SaveChangesAsync();
            return res;
        }
        public async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
        {
            var dbSet = _context.Set<TEntity>();
            return await dbSet.FirstOrDefaultAsync(predicate);
        }

        public async Task<int> UpdateResultIdAsync(TEntity entity)
        {
            var res = _context.Set<TEntity>().Update(entity);
            await _context.SaveChangesAsync();
            return res.Entity.Id;
        }
        public async Task<TEntity> UpdateAsync(TEntity entity)
        {
            var dbSet = _context.Set<TEntity>();
            var res = dbSet.Update(entity).Entity;
            await _context.SaveChangesAsync();
            return res;
        }

        public async Task<TEntity> DeleteAsync(TEntity entity)
        {
            var dbSet = _context.Set<TEntity>();
            var res = dbSet.Remove(entity).Entity;
            await _context.SaveChangesAsync();
            return res;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<TEntity>> GetListAsync()
        {
            var dbSet = _context.Set<TEntity>();
            return await dbSet.ToListAsync();
        }

        public async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate)
        {
            var dbSet = _context.Set<TEntity>();
            return await dbSet.Where(predicate).ToListAsync();
        }

        public async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, string sort, int pageIndex, int pageSize)
        {
            int skip = (pageIndex - 1) * pageSize;
            var dbSet = _context.Set<TEntity>();
            return await dbSet.Where(predicate).OrderBy(m => sort).Skip(skip).Take(pageSize).ToListAsync();
        }

        public async Task<TEntity> GetEntityByIdAsync(int id)
        {
            var dbSet = _context.Set<TEntity>();
            return await dbSet.FirstOrDefaultAsync(m => m.Id == id);
        }
    }
}
